//
//  ViewController.m
//  Dukpt4Ios
//
//  Created by zengqingfu on 15/3/15.
//  Copyright (c) 2015年 zengqingfu. All rights reserved.
//

#import "ViewController.h"
#import "DUKPT_2009_CBC.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //NSLog(@"Hello, World!");
    
    DUKPT_2009_CBC *dukpt = [[DUKPT_2009_CBC alloc] init];
    
    NSString *ksn = @"00000332100300E00004";
    NSString *bdk = @"0123456789ABCDEFFEDCBA9876543210";
    NSData *byte_ksn = [dukpt parseHexStr2Byte:ksn];
    NSData *byte_bdk = [dukpt parseHexStr2Byte:bdk];
    
    NSData *ipek = [dukpt GenerateIPEKksn:byte_ksn bdk:byte_bdk];
    NSString *ipekStr = [dukpt parseByte2HexStr:ipek];
    NSLog(@"ipekStr = %@", ipekStr);
    
    NSData *dataKey = [dukpt GetDataKeyVariantKsn:byte_ksn ipek:ipek];
    NSString *dataKeyStr = [dukpt parseByte2HexStr:dataKey];
    NSLog(@"dataKeyStr = %@", dataKeyStr);
    
    NSData *dataKey__ = [dukpt GetDataKeyKsn:byte_ksn ipek:ipek];
    NSString *dataKeyStr__ = [dukpt parseByte2HexStr:dataKey__];
    NSLog(@"dataKeyStr__= %@", dataKeyStr__);
    
    NSString *dataStr = @"BF8544CAD8E1C8FD8B87483C44C7637E8D885C0806D40254";
    NSData *temDecData = [DUKPT_2009_CBC DESOperationCBCdata:[dukpt parseHexStr2Byte:dataStr] key:[dukpt parseHexStr2Byte:dataKeyStr__]];
    
    NSString *deResultStr = [dukpt parseByte2HexStr:temDecData];
    NSLog(@"data: %@", deResultStr);
    
    ksn = @"00000332100300E00008";
    bdk = @"0123456789ABCDEFFEDCBA9876543210";
    byte_ksn = [dukpt parseHexStr2Byte:ksn];
    byte_bdk = [dukpt parseHexStr2Byte:bdk];
    
    ipek = [dukpt GenerateIPEKksn:byte_ksn bdk:byte_bdk];
    ipekStr = [dukpt parseByte2HexStr:ipek];
    NSLog(@"ipekStr = %@", ipekStr);
    
    NSData *pinKey = [dukpt GetPinKeyVariantKsn:byte_ksn ipek:ipek];
    NSString *pinKeyStr = [dukpt parseByte2HexStr:pinKey];
    NSLog(@"pinKeyStr = %@", pinKeyStr);
    
    dataStr = @"A5F3FCB015C3D9C7";
    NSData *desData = [DUKPT_2009_CBC DESOperation:kCCDecrypt algorithm:kCCAlgorithm3DES keySize:kCCKeySize3DES data:[dukpt parseHexStr2Byte:dataStr] key:[dukpt parseHexStr2Byte:pinKeyStr]];
    deResultStr = [dukpt parseByte2HexStr:desData];
    NSLog(@"pin: %@", deResultStr);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
